/*
 * TaskItem.java
 *
 * Created on 23.06.2008, 14:14
 *
 * Copyright (c) 2006-2008, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package Tasks;

import images.RosterIcons;
import ui.IconTextElement;

/**
 *
 * @author ad
 */
public class TaskItem extends IconTextElement {
    private int type;
    private String source="";
    private String dest="";
    private String attr="";
    
    /** Creates a new instance of TaskItem */
    public TaskItem(String from, String to, int type, String attr) {
        super(RosterIcons.getInstance());
        source=from;
        dest=to;
        this.type=type;
        this.attr=attr;
    }
    
    public String toString(){
        StringBuffer s=new StringBuffer().append(type).append(": ");
        s.append(prepareString(source));
        s.append(" - ");
        s.append(prepareString(dest));
        return s.toString();
    }
    
    public String prepareString(String value) {
        if (value.equals(""))
            return "";
        
        StringBuffer s=new StringBuffer();
        s.append(value.substring(0, value.lastIndexOf('/')).toUpperCase());
        s.append(value.substring(value.lastIndexOf('/')));
        
        return s.toString();
    }

    public int getImageIndex() { return type;}

    public void setType(int type) {
        this.type=type;
    }

    public void setSource(String from) {
        source=from;
    }

    public void setDest(String to) {
        dest=to;
    }

    public void setAttr(String attr) {
        this.attr=attr;
    }

    int getType() {
        return type;
    }

    String getTo() {
        return dest;
    }

    String getFrom() {
        return source;
    }

    String getAttr() {
        return attr;
    }
    
    public String getAction() {
        StringBuffer action=new StringBuffer();
        action.append("<file-op parse-mask=\"fa\" op=\"");
        switch (type) {
            case 0: //copy
                action.append("copy\" ");
                if (source=="" || dest =="")
                    return null;
                action.append("source=\"").append(prepareString(source)).append("\" ").append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 1: //delete
                action.append("delete\" ");
                if (dest =="")
                    return null;
                action.append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 2: //purgedir
                action.append("purgedir\" ");
                if (dest =="")
                    return null;
                action.append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 3: //chmod
                action.append("chmod\" "); // TODO attrs
                if (dest =="" || attr=="")
                    return null;
                action.append("dest=\"").append(prepareString(dest)).append("\" attr=\"").append(attr).append("\"");
                break;
            case 4: //link
                action.append("link\" ");
                if (source=="" || dest =="")
                    return null;
                action.append("source=\"").append(prepareString(source)).append("\" ").append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 5: //move
                action.append("move\" ");
                if (source=="" || dest =="")
                    return null;
                action.append("source=\"").append(prepareString(source)).append("\" ").append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 6: //rename
                action.append("rename\" ");
                if (source=="" || dest =="")
                    return null;
                action.append("source=\"").append(prepareString(source)).append("\" ").append("dest=\"").append(prepareString(dest)).append("\"");
                break;
            case 7: //cleardir
                action.append("cleardir\" ");
                if (dest =="")
                    return null;
                action.append("dest=\"").append(prepareString(dest)).append("\"");
                break;
        }
        action.append("/>");
        return action.toString();
    }
    
/* example
    <?xml version="1.0" encoding="UTF-8" ?>
    <customization>
    <file-operations>
    <file-op parse-mask="fa" op="purgedir" dest="usb/picture/test"/>
    <file-op parse-mask="fa" op="copy" source="usb/picture/a2.txt" dest="tpa/user/other/usbtotpa.txt"/>
    </file-operations>
    </customization>
 *
    <file-op parse-mask=\"fa\" op=\"copy\" source=\"path/app.jad "\" dest=\"" + "TPA/PRESET/DEFAULT/JAVA/app.jad\" />
    <file-op parse-mask=\"fa\" op=\"copy\" source=\"path/app.jar "\" dest=\"" + "TPA/PRESET/DEFAULT/JAVA/app.jar\" />
    <midlet deletable=\"false\" upgradeable=\"true\" install=\"true\" folder=\"applications\">
    <jad-file>java/app.jad</jad-file>
    <jar-file>java/app.jar</jar-file>
    </midlet>
 *
    copy — copy files
    delete — delete files
    purgedir — delete folder and files in it
    chmod — change attrs
    link — make link
    move  — move
    rename  — rename
    cleardir  — clear folder
 *
    Usb/ memory K790/K800/k550
    Tpa/user/ memory not for K790/K800/k550
    Card/ memorycard.
 */ 
}
